<?php include_once "../ra_global.php";
//添加店铺
if($_POST['add_shop']){
	$code=$db->ig2_want('mae_shop','code="'.$_POST['code'].'"');
	if($code){
		tellgoto('店铺代码重复','shop_edit.php');
	}else{
		$value=array(
			'tid'					=>'',
			'id'					=>rand(1000,9999).date('YmdHis',time()),
			'account_id'			=>16621,
			'admin_id'				=>0,
			'repairman_limit'		=>5,
			'salesman_limit'		=>0,
			'forever_qrcode'		=>'',
			'qrcode_image'			=>'',
			'forever_qrcode_detail'	=>'',
			'code'					=>$_POST['code'],
			'name'					=>$_POST['name'],
			'city_id'				=>$_POST['city_id'],
			'sell_count'			=>0,
			'now_month_count'		=>0,
			'prev_month_count'		=>0,
			'used_count'			=>0,
			'quadratic'				=>0,
			'is_activate'			=>0,
			'create_time'			=>time(),
			'activate_time'			=>'',
			'status'                =>1,
		);
		$shop=is_exists('id','mae_shop',$value);
		$tid=$shop['tid'];
		
		if($tid){
			
			//获取access_token
			$id='shell';
			$wxlist=array(
					"shell"		=>array(APPID,APPSECRET),
			);
			$appid = $wxlist[$id][0];
			$appsecret = $wxlist[$id][1];
			
			//Memcache方法
			$mem = new Memcache;
			$mem->connect('localhost', 11211) or die ("Could not connect");
			$access_token = $mem->get($id);
			if (empty($access_token)){
				$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
				$res = http_request($url);
				$result = json_decode($res, true);
				$access_token = $result["access_token"];
				$mem->set($id, $access_token, 0, 7000);
			}
			
			//普通方法
			/* $access_token_req=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;);
			$access_token_ret=json_decode($access_token_req);
			$access_token=$access_token_ret->access_token; */
			
			//获取店员二维码
			$json='
				{
					"action_name":"QR_LIMIT_STR_SCENE",
					"action_info":{
						"scene":{
							"scene_str":"'.trim($_POST['code']).'"
						}
					}
				}
			';
			
			$ticket_req=ticket_req($access_token,$json);
			$ticket_ret=json_decode($ticket_req);
			$ticket=UrlEncode($ticket_ret->ticket);
			
			$url='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket;
			$filename=$tid.'.jpg';
			$dirname = "../repaircode/";
			
			$qrcode_image=getCode($url,$filename,$dirname);
			
			//设置需要发送的json字符串
			$json='
				{
					"action_name":"QR_LIMIT_SCENE",
					"action_info":{
						"scene":{
							"scene_id":'.$tid.'
						}
					}
				}
			';
			
			//获取ticket
			$ticket_req=ticket_req($access_token,$json);
			$ticket_ret=json_decode($ticket_req);
			$ticket=UrlEncode($ticket_ret->ticket);
			
			$url='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket;
			$filename=$tid.'.jpg';
			$dirname = "../shopcode/";
			
			//下载二维码到本地
			$qrcode_image=str_replace('../','',getCode($url,$filename,$dirname));
			
			$update_value=array(
				'forever_qrcode'		=>$ticket,
				'qrcode_image'			=>$qrcode_image,
				'forever_qrcode_detail'	=>$ticket_req,
			);
			
			$db->ig2_update('mae_shop',$update_value,'tid='.$tid);
			
		}
		
		tellgoto('添加成功','show.php?tid='.$tid);
	}
	
}

//添加商品
if($_POST['product_add']){
	if($_FILES['img']['tmp_name']){
		$suffix=pathinfo($_FILES['img']['name'],PATHINFO_EXTENSION);//获取文件后缀名
		$img=date('YmdHis').rand(1000,9999).'.'.$suffix;
		move_uploaded_file($_FILES['img']['tmp_name'],'../prize/'.$img);
	}
	$value=array(
		'id'			=>'',
		'name'			=>$_POST['name'],
		'need_integral'	=>$_POST['need_integral'],
		'img'			=>$img,
		'is_virtual'	=>$_POST['is_virtual'],
		'stock'			=>$_POST['stock'],
		'status'		=>$_POST['status'],
		'describes'		=>str_replace("'","&apos;",$_POST['describes']),
		'create_time'	=>time(),
	);
	$id=$db->ig2_insert('mae_prize',$value);
	if($id) tellgoto('添加商品成功','products.php');
	else  tellgoto('添加商品失败','product_edit.php');
}
//修改商品信息
if($_POST['product_edit']){
	$prize=$db->ig2_want('mae_prize','id='.$_POST['id']);
	if(!$prize){
		tellgoto('访问错误','products.php');
		exit();
	}
	if($_FILES['img']['tmp_name']){
		$suffix=pathinfo($_FILES['img']['name'],PATHINFO_EXTENSION);//获取文件后缀名
		$img=date('YmdHis').rand(1000,9999).'.'.$suffix;
		move_uploaded_file($_FILES['img']['tmp_name'],'../prize/'.$img);
	}else{
		$img=$prize['img'];
	}
	$value=array(
		'name'			=>$_POST['name'],
		'need_integral'	=>$_POST['need_integral'],
		'img'			=>$img,
		'stock'			=>$_POST['stock'],
		'status'		=>$_POST['status'],
		'describes'		=>str_replace("'","&apos;",$_POST['describes']),
	);
	$db->ig2_update('mae_prize',$value,'id='.$prize['id']);
	tellgoto('修改商品信息成功','products.php');
}
?>